package com.whenjun.service.core;

import com.alibaba.fastjson.JSONObject;
import com.whenjun.common.LoggerParent;
import com.whenjun.entity.IscDoorRecord;
import com.whenjun.service.impl.IscDoorRecordServiceImpl;
import com.whenjun.service.impl.KqAlarmServiceImpl;
import com.whenjun.thread.AsyncServiceEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: shirley
 * @Date: 2022/2/13 15:33
 * @Description: 接受事件处理
 */
@Service
public class RecService extends LoggerParent {

    @Autowired
    private DoorService doorService;

    @Autowired
    private IscDoorRecordServiceImpl iscDoorRecordService;

    @Autowired
    private KqAlarmServiceImpl kqAlarmService;

    @Autowired
    private AsyncServiceEvent asyncServiceEvent;


    public void eventDeal(JSONObject jsonObject) {
        JSONObject data = jsonObject.getJSONObject("params").getJSONArray("events").getJSONObject(0);
        Integer eventType = data.getInteger("eventType");

        // 事件集合 ：https://open.hikvision.com/docs/docId?productId=5c67f1e2f05948198c909700&curNodeId=8a705babb4434ba48b876427d7a3ab94
        switch (eventType) {
            case 196893: // 	人脸认证通过
                log.info("eventType: 人脸认证通过 196893");
                preDoControl(data);
                IscDoorRecord faceRecord = iscDoorRecordService.record(data);

                // 异步执行
                asyncServiceEvent.calcFloorRestTime(faceRecord);
                asyncServiceEvent.calcWcRestTime(faceRecord);
                asyncServiceEvent.calcOneRestTime(faceRecord);

                break;
//            case 197390:
//                log.info("eventType: 认证成功等待远程开门 197390");
//                preDoControl(data);
//                break;
            case 198914:
                log.info("eventType: 合法卡比对通过 198914");
                preDoControl(data);
                IscDoorRecord cadRecord = iscDoorRecordService.record(data);

                // 异步执行
                asyncServiceEvent.calcFloorRestTime(cadRecord);
                asyncServiceEvent.calcWcRestTime(cadRecord);
                asyncServiceEvent.calcOneRestTime(cadRecord);

                break;
            case 1644175361:
                System.out.println("人脸重点事件");

                IscDoorRecord iptFaceRecord = iscDoorRecordService.recordIptFace(data);

                // 异步执行
                asyncServiceEvent.calcFloorRestTime(iptFaceRecord);
                asyncServiceEvent.calcWcRestTime(iptFaceRecord);
                asyncServiceEvent.calcOneRestTime(iptFaceRecord);

                break;
            default:
                break;
        }
    }

    /**
     * @Author shirley
     * @Description  preDo
     * @Date 11:10 2022/7/10
     * @Param [data]
     * @return void
     **/
    public void preDoControl(JSONObject data){
        // 获取道闸编号
        String code = data.getString("srcIndex");

        JSONObject reqbody = new JSONObject();
        List<String> codes = new ArrayList<>();
        codes.add(code);
        reqbody.put("doorIndexCodes",codes);

        // 0: 常开
        //1: 门闭
        //2: 门开
        //3: 常闭
        reqbody.put("controlType",2);
        doorService.doControl(reqbody);
    }
}
